<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{

    public function getLists($cate = '女装', $page = 0, $pageSize = 10)
    {
        $res = $this->offset($page * $pageSize)->limit($pageSize)->orderBy('status', 'DESC')->orderBy('sort', 'DESC')->orderBy('view', 'DESC')
            ->orderBy('sales_month', 'DESC')->orderBy('id', 'DESC')
            ->where('category', $cate)->get();
        return $res;
    }

    public function getCateListsRand($cate = '', $num = 100)
    {
        if (empty($cate)) {
            $ids = getShuffleRand($this->lists('id')->toArray(), $num);
            /*$generIds = $this->where('view', '>', 0)->lists('id')->toArray();
            $ids = array_collapse([$ids, $generIds]);*/
            $res = $this->whereIn('id', $ids)->orderBy('view', 'DESC')->orderBy('commission_percent', 'DESC')->get();
        } else {
            $ids = getShuffleRand($this->where('category', $cate)->lists('id')->toArray(), $num);
            /*$generIds = $this->where('sort', '>', 0)->where('category', $cate)->lists('id')->toArray();
            $ids = array_collapse([$ids, $generIds]);*/
            $res = $this->whereIn('id', $ids)->where('category', $cate)
                ->orderBy('view', 'DESC')->orderBy('commission_percent', 'DESC')->orderBy('id', 'DESC')->get();
        }
        return $res;
    }

    public function getGoodsRand($num = 1)
    {
        $ids = getShuffleRand($this->lists('id')->toArray(), $num);
        $res = $this->whereIn('id', $ids)->get();
        return $res;
    }

    public function getGoodsTpwd($num = 1)
    {
        $ids = getShuffleRand($this->lists('id')->toArray(), $num);
        $res = $this->whereIn('id', $ids)->get();
        return $res;
    }

    public function getCategoryArray()
    {
        $res = Goods::select('category')->groupBy('category')->get();
        foreach ($res as $key => $obj) {
            $obj->category = str_replace('/', '-', $obj->category);
        }
        $res = $res->toArray();
        $data['list1'] = array_chunk($res, 1);
        $data['list2'] = array_chunk($res, 2);
        $data['list3'] = array_chunk($res, 3);
        $data['list4'] = array_chunk($res, 4);
        $data['list5'] = array_chunk($res, 5);
        $data['list6'] = array_chunk($res, 6);
        $data['list7'] = array_chunk($res, 7);
        $data['list8'] = array_chunk($res, 8);
        $data['list9'] = array_chunk($res, 9);
        $data['list10'] = array_chunk($res, 10);
        return $data;
    }

    public function getGoodsData($cate = 0)
    {
        $ids = getShuffleRand($this->lists('id')->toArray(), 150);
        $res = $this->whereIn('id', $ids)->select('id', 'title', 'coupons_amount', 'cover as path', 'generalize_link')
            ->orderBy('status', 'DESC')->orderBy('sort', 'DESC')->orderBy('view', 'DESC')->orderBy('sales_month', 'DESC')
            ->orderBy('id', 'DESC')->get();
        $res = $res->toArray();
        $data['list1'] = array_chunk($res, 1);
        $data['list2'] = array_chunk($res, 2);
        $data['list3'] = array_chunk($res, 3);
        $data['list4'] = array_chunk($res, 4);
        $data['list5'] = array_chunk($res, 5);
        $data['list6'] = array_chunk($res, 6);
        $data['list7'] = array_chunk($res, 7);
        $data['list8'] = array_chunk($res, 8);
        $data['list9'] = array_chunk($res, 9);
        $data['list10'] = array_chunk($res, 10);
        return $data;
    }

    public function getClientGoods($gids)
    {
        $res = $this->whereIn('gid', $gids)->select('id', 'gid', 'title', 'coupons_amount', 'cover as path', 'generalize_link')
            ->orderBy('status', 'DESC')->orderBy('sort', 'DESC')->orderBy('view', 'DESC')->orderBy('sales_month', 'DESC')->orderBy('id', 'DESC')->get();
        $res = $res->toArray();
        return $res;
    }
}
